package com.example.jmh;

import org.apache.commons.beanutils.BeanUtils;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@BenchmarkMode(value = {Mode.SingleShotTime})
@State(Scope.Thread)
@Fork(1)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Warmup(iterations = 3)
@Measurement(iterations = 5)
public class TestBean {

    public static void main(String[] args) throws RunnerException {
        Options options = new OptionsBuilder()
                .include(TestBean.class.getSimpleName())
                .build();
        new Runner(options).run();
    }

    @Benchmark
    public void testSpring() throws InvocationTargetException, IllegalAccessException {
        List<SubEntity> subEntityList = new ArrayList<>();
        Entity entity = new Entity("张三",12);
        for(int i = 0;i<100;i++){
            subEntityList.add(new SubEntity());
        }
        entity.setSubEntityList(subEntityList);
        entity.setSubEntity(new SubEntity());
        for(int i=0 ;i<100;i++){
            Entity e = new Entity();
            BeanUtils.copyProperties(e,entity);
        }
    }

    @Benchmark
    public void testCommon(){
        List<SubEntity> subEntityList = new ArrayList<>();
        Entity entity = new Entity("张三",12);
        for(int i = 0;i<100;i++){
            subEntityList.add(new SubEntity());
        }
        entity.setSubEntityList(subEntityList);
        entity.setSubEntity(new SubEntity());
        for(int i=0 ;i<100;i++){
            Entity e = new Entity();
            org.springframework.beans.BeanUtils.copyProperties(entity,e);
        }

    }
}
